package com.showcai.controller.certificate;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.CertificateClassify;
import com.showcai.module.Product;
import com.showcai.module.ProductEntity;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.CertificateClassifyService;
import com.showcai.service.ProductEntityService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-7-3.
 */
@RequestMapping("/certificateClassify")
@Controller
public class CertificateClassifyContorller extends BaseController {
    @Autowired
    CertificateClassifyService certificateClassifyService;
    @Autowired
    ProductEntityService productEntityService;

    /**
     * 卡劵管理
     * <p>
     * //     * @param request
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/certificateClassifyList")
    public ModelAndView certificateClassifyList(Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/certificate/certificateClassifyList");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        PageInfo pageInfo = certificateClassifyService.getPageInfoCertificateClassifyList(map);
        mov.addObject("pageinfo", pageInfo);
        mov.addObject("bean", map);
        return mov;
    }


    /**
     * 跳转到卡劵修改页面
     *
     * @param certificateClassifyId
     * @return
     */
    @RequestMapping(value = "/certificateClassify")
    public ModelAndView certificateClassify(Long certificateClassifyId) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/certificate/certificateClassify");
        if (certificateClassifyId != null) {

            CertificateClassify certificateClassify = certificateClassifyService.selectCertificateClassifyById(certificateClassifyId);
            mov.addObject("certificateClassify", certificateClassify);
        }
        Map<String, String> map = new HashMap<>();
        map.put("productClassifyId", String.valueOf(Product.PRODUCT_CLASSIFY_CERTIFICATE));

        List<ProductEntity> productEntityList = productEntityService.getProductEntityList(map);
        mov.addObject("productEntityList", productEntityList);
        return mov;
    }

    /**
     * 修改卡券方法
     *
     * @param certificateClassify
     * @return
     */
    @RequestMapping(value = "/modifyCertificateClassify", method = RequestMethod.POST)
    @ResponseBody
    public String modifyCertificateClassify(@RequestParam(value = "photo", required = false) MultipartFile photo, CertificateClassify certificateClassify) {
        ResultObj resultObj = new ResultObj();
        try {
            String filePath = "showcai/certificate/";
           /* Map<String, String> fileMap=  super.upLoadFile(photo.getInputStream(),filePath);
            for (String key : fileMap.keySet()) {
                System.out.println("Key = " + key);
            }*/
           if(photo!=null){

               String photos = super.upLoadFile(photo.getInputStream(), filePath);
               if (StringUtils.isNotBlank(photos)) {
                   certificateClassify.setCertificatePic(photos);
               }
           }
            certificateClassifyService.ModifyCertificateClassify(certificateClassify);

        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

}
